﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.DataAccess;
using TVGuide.DataAccess.Common;
using TVGuide.DataAccess.Exceptions;

namespace TVGuide.DataAccess
{
    public static class Utilsities
    {
        // Adds categorized items to programs table
        // not categorized items to notcatprograms table
        // inherits category from channel if channel is categorized
        public static void addPrograms(List<Program> toAdd)
        {
            tvchanneldbEntities model = new tvchanneldbEntities();
            foreach (var item in toAdd)
            {
                if (item == null)
                    throw new InvalidArgsException("The parameter is null");
                if (model.channels.FirstOrDefault(o => o.channelID == item.ChannelId) == null)
                    throw new InvalidArgsException("Invalid channelId");

                DateTime dt = new DateTime(2000, 01, 01);
                if (item.Date < dt)
                    throw new InvalidArgsException("The 'date'property is null");
                if (String.IsNullOrEmpty(item.Name))
                    throw new InvalidArgsException("The 'title' property is null or empty");
                
                // Temporary hiding additional check
                /*if (item.CountryId != null)
                {
                    try
                    {
                        if (model.countries.FirstOrDefault(o => o.countryID == item.CountryId) == null)
                            throw new InvalidArgsException("Invalid countryId");
                    }
                    catch (InvalidOperationException)
                    {
                        throw new InvalidArgsException("Invalid countryId");
                    }
                }*/
                // Do not add duplicates
                if (model.programs.FirstOrDefault(o => o.channelID == item.ChannelId && o.date == item.Date) != null)
                {
                    continue;
                }

                if (item.CategoryId != null)
                {
                    if (model.categories.FirstOrDefault(o => o.catID == item.CategoryId) == null)
                        throw new InvalidArgsException("Invalid categoryId");
                }
                else
                {   // Inheriting categoryId from channel
                    var channel = model.channels.FirstOrDefault(o => o.channelID == item.ChannelId);
                    if (channel != null && channel.categoryID != null)
                    {
                        item.CategoryId = channel.categoryID;
                        if(model.catPrograms.FirstOrDefault(o => o.channelID == item.ChannelId && o.programName == item.Name) == null)
                            model.catPrograms.AddObject(new catProgram() { categoryID = (short)item.CategoryId, channelID = item.ChannelId, programName = item.Name });
                    }
                    else
                    {
                        autoCategorize(item);
                        if (item.CategoryId != null)
                        {
                            model.programs.AddObject((program)item);
                            model.SaveChanges();
                            continue;
                        }
                        var prog = model.catPrograms.FirstOrDefault(o => o.programName == item.Name);
                        if (prog != null)
                        {
                            item.CategoryId = prog.categoryID;
                        }
                        else
                        {
                            var obj = model.notCatPrograms.FirstOrDefault(o => (o.title == item.Name && o.channelID == item.ChannelId));
                            
                            // This program doesn't exist in notCatPrograms
                            if (obj == null)
                            {
                                // If no category foud, add to notcatprograms table.
                                model.notCatPrograms.AddObject(new notCatProgram()
                                {
                                    categoryID = item.CategoryId,
                                    channelID = item.ChannelId,
                                    countryID = item.CountryId,
                                    date = item.Date,
                                    description = item.Description,
                                    languageID = item.LanguageId,
                                    title = item.Name
                                }); 
                                
                            }
                        }
                    }
                }
                model.programs.AddObject((program)item);
                model.SaveChanges();
            }
            model.SaveChanges();
        }

        private static void autoCategorize(Program p)
        {
            // Films
            if (p.Name.ToLower().Contains("գֆ") || p.Name.ToLower().Contains("վ/ֆ") || 
                p.Name.ToLower().Contains("գ/ֆ") || p.Name.ToLower().Contains("գեղ/ֆիլմ") ||
                p.Name.ToLower().Contains("в ролях") || p.Name.ToLower().Contains("в главной роли ")||
                p.Name.ToLower().Contains("в главных ролях ") || p.Name.ToLower().Contains("գեղարվեստական ֆիլմ")||
                p.Name.ToLower().Contains("փ/ֆ") ||p.Name.ToLower().Contains("գեղֆիլմ")

                )
            {
                p.CategoryId = (short)Enums.Categories.FILMS;
            }
            //Children 
            if (p.Name.ToLower().Contains("мультсериал") || p.Name.ToLower().Contains("мультфильм")||
               p.Name.ToLower().Contains("мультпарад") || p.Name.ToLower().Contains("մ/ֆ") ||
               p.Name.ToLower().Contains("մֆ") || p.Name.ToLower().Contains("մ/ս")
                )
            {
                p.CategoryId = (short)Enums.Categories.CHILDREN;
            }

            //Sport 
            if (p.Name.ToLower().Contains("футбол") || p.Name.ToLower().Contains("профессиональный бокс")||
                p.Name.ToLower().Contains("пулевая стрельба") || p.Name.ToLower().Contains("легкая атлетика")||
                p.Name.ToLower().Contains("смешанные единоборства") || p.Name.ToLower().Contains("регби-7")||
                p.Name.ToLower().Contains("волейбол") || p.Name.ToLower().Contains("ֆուտբոլ.անգլիայի պլ.") ||
                p.Name.ToLower().Contains("ֆուտբոլ.") || p.Name.ToLower().Contains("ֆուտբոլ")
                
                )
            {
                p.CategoryId = (short)Enums.Categories.SPORT;
            }

            //Humor  


            //Music 
            if (p.Name.ToLower().Contains("концерт")

                )
            {
                p.CategoryId = (short)Enums.Categories.MUSIC;
            }
        }

        public static void moveCatProgsToPrograms()
        {
            tvchanneldbEntities model = new tvchanneldbEntities();
            foreach (var item in model.notCatPrograms)
            {
                if (item.categoryID >= 0)
                {
                    var progItems = model.programs.Where(o => o.channelID == item.channelID && o.title == item.title);
                    foreach(var progItem in progItems)
                        progItem.categoryID = item.categoryID;

                    if(model.catPrograms.FirstOrDefault(o => o.programName == item.title && o.channelID == item.channelID) == null)
                        model.catPrograms.AddObject(new catProgram() { categoryID = (short)item.categoryID, programName = item.title, channelID = item.channelID });
                    model.notCatPrograms.DeleteObject(item);
                }
            }
            model.SaveChanges();
        }
    }
 
}
